Page({
  data: {
    list: [],
    imgUrls: [],
    swiperIndex: 0, //这里不写第一次启动展示的时候会有问
    num: 1, //数量
    goodid: '',
    goodname: '',
    oldprice: 0, //原价
    nowprice: 0, //现价
    contents: '',
    total_price: '', //总价
    goodid: '',
  },
  bindchange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  swiperChange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  getLunbo: function() {
    var that = this;
   
    var req = {
      id: that.data.goodid
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/getgood', // 该url是自己的服务地址
      method: 'POST',
      headers: {
        // 'x-access-token': that.data.token
      },
      data: req,
      dataType: 'json',
      success: (res) => {// 成功获取订单号
        if (res.data.status == 200) {
          var img = [];
          for (var i = 0; i < res.data.data.imglist.length; i++) {
            img.push(res.data.data.imglist[i])
          }
          that.setData({
            imgUrls: img,
            contents: res.data.data.contents,
            oldprice: res.data.data.oldprice,
            nowprice: res.data.data.nowprice,
            goodname: res.data.data.goodname,
            goodid: res.data.data.id
          })
          that.post()

        } else {
          my.showToast({
            success: (res) => {
              content: res.data.message
            }
          });
        }


      },
      fail: (res) => {
        console.log('GetEmpPrice:', res)
      },
    });
  },
  post: function() {
    var that = this;
    console.log(that.data.userId)
    if (that.data.userId == ''||that.data.userId==null) {
      my.navigateTo({
        url: '/pages/login/login'
      })
    }
    var req = {
      id: that.data.goodid,
      userid: that.data.userId
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/getcoupon', // 该url是自己的服务地址
      method: 'POST',
      headers: {
        // 'x-access-token': that.data.token
      },
      data: req,
      dataType: 'json',
      success: (res) => {// 成功获取订单号
        if (res.data.status == 200) {
          var arr = [];
          for (var i = 0; i < res.data.data.length; i++) {
            arr.push(res.data.data[i])
          }

          that.setData({
            list: arr,
            hidden:false
          })

        } else {
          my.showToast({
            success: (res) => {
              content: res.data.message
            }
          });
        }


      },
      fail: (res) => {
        console.log('GetEmpPrice:', res)
      },
    });
  },
  // 获取token
  getToken() {
    var that = this;
    my.getAuthCode({
      scopes: 'auth_user', //授权类型，默认auth_base(静默授权)，支持auth_base/auth_user(主动授权)
      success: (res) => {
        if (res.authCode) { // 认证成功
          my.request({ // 成功之后调用后台开启设备接口
            url: 'https://www.tiantong369.com/gettoken',
            method: 'POST',
            data: { code: res.authCode },
            success: (res) => {
              if (res.status == 200) {
                my.setStorageSync({ key: 'token', data: '' + res.data.token + '' })
                that.getLunbo()
              }
            },
            fail: (res) => {
              console.log('fail:', res)
            },
          });
        }
      },
    })
  },

  receive: function(e) {
    var that = this;
    var req = {
      couponid: e.currentTarget.dataset.id,
      userid: that.data.userId
    }
    my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      url: 'https://www.tiantong369.com/crm/usergetcoupon', // 该url是自己的服务地址
      method: 'POST',
      headers: {
        // 'x-access-token': that.data.token
      },
      data: req,
      dataType: 'json',
      success: (res) => {// 成功获取订单号
        if (res.data.status == 200) {
          that.post();
          my.showToast({
            content: res.data.message,
            duration: 2000,

          });
        } else {
          my.showToast({
            content: res.data.message

          });
        }


      },
      fail: (res) => {
        console.log('GetEmpPrice:', res)
      },
    });
  },
  add: function() {
    var that = this;
    that.data.num++
    this.setData({
      num: that.data.num,
      total_price: that.data.nowprice * that.data.num,
    })
  },
  reduce: function() {
    if (this.data.num <= 1) {
      return;
    }
    this.data.num -= 1;
    this.setData({
      num: this.data.num,
      total_price: this.data.nowprice * this.data.num,
    })
  },
  //购买
  buy: function() {
    my.navigateTo({
      url: '/pages/goodsDetail1/goodsDetail1'
    })
  },
  onLoad(query) {
    // 页面加载
    var that = this;
    that.setData({
      goodid: query.goodid
    })
  },

  onShow() {
    var that = this;
    my.getNetworkType({
      success: (res) => {
        if (res.networkAvailable) {
          that.setData({
            outline: false,
          })
             that.getToken()
          //获取useid
          my.getStorage({
            key: 'userId',
            success: function(res) {
              if (res.data == null) {
                that.setData({
                  userId: ''
                })
              } else {
                that.setData({
                  userId: res.data
                })
              }
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          // 获取token
          my.getStorage({
            key: 'token',
            success: function(res) {
              that.setData({
                token: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
        } else {
          that.setData({
            outline: true
          })
        }
      }
    })
  },

  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  bindKeyInput(e) {
    this.setData({
      code: e.detail.value
    });
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
  // ------------------- 分割线 --------------------
  onReady: function() {

  },

});
